Online-Academy
Look, Read, Understand, Apply

Q and A Junly 2026

Short Questions

  1. Explain the difference between == and .equals() in Java with respect to string comparison. When would you use each?
    • == compares memory references (if both point to same object)
    • .equals() compares content or value of the strings
    • Use == for primitive comparisons or checking if two references point to the same object
    • Use .equals() for comparing string content
    • Example: Hello == Hello may be true due to string pooling, but always use .equals() for content comparison
  2. What is the Java Virtual Machine (JVM)? Why is Java called a platform-independent language? Explain the role of bytecode in achieving platform independence.
    • JVM: An abstract machine that executes Java bytecode
    • Platform independence: Java code compiles to bytecode (.class files), which can run on any platform with a JVM
    • Bytecode: Intermediate representation that is platform-independent; JVM translates bytecode to machine-specific instructions
  3. What are the different primitive data types in Java? Explain their memory sizes. Why is String not considered a primitive type?
    Type	Size	Range
    byte	1 byte	-128 to 127
    short	2 bytes	-32,768 to 32,767
    int	    4 bytes	-231 to 231-1
    long	8 bytes	-263 to 263-1
    float	4 bytes	+-3.4e-38 to +-3.4e+38
    double	8 bytes	+-1.7e-308 to +-1.7e+308
    char	2 bytes	0 to 65,535 (Unicode)
    boolean	1 bit	true or false
    String is a class (reference type), not primitive
    
  4. What is encapsulation in Java? How do you achieve encapsulation in a class? What are the benefits of using encapsulation in object-oriented programming?
    • Encapsulation: Wrapping data and methods into a single unit (class), hiding internal details
    • Achieved by: private fields + public getter or setter methods
    • Benefits:
    • Data hiding and security
    • Controlled access
    • Modularity
    • Easy maintenance and testing
  5. Explain the difference between:
    A class and an object
    An instance variable and a local variable
    A static method and an instance method.
    Class: Blueprint or template	Object: Instance of class
    Instance: belongs to object, has default values	
    Local: declared inside method, no default values
    Static: belongs to class, can be called without object
    Instance: belongs to object, requires object to call
  6. What is inheritance in Java? Explain the concept of IS-A relationship. Why is inheritance considered an important pillar of object-oriented programming? What are its advantages and disadvantages?
    • Inheritance: Mechanism where one class acquires properties or behaviors of another
    • IS-A relationship: Child IS-A type of parent (e.g., Dog IS-A Animal)
    • Advantages: Code reusability, method overriding, extensibility
    • Disadvantages: Tight coupling, fragile base class problem, deeper hierarchies are complex
  7. What is the difference between super and this keywords in Java? When and why would you use super() in a constructor? Can you call super() and this() in the same constructor? Why or why not?
    • super: Refers to parent class object
      this: Refers to current object
    • super: Used to access parent methods or variables
      this:Used to access current class methods or variables
    • super(): calls parent constructor
      this(): calls current class constructor
    • Cannot use both super() and this() in same constructor (both must be first statement)
  8. What is method overriding in Java? List the rules for method overriding. What is the difference between method overriding and method hiding? What happens if you use @Override annotation incorrectly?
    • Method Overriding Rules:
    • Method name and parameters must match parent
    • Return type must be same or covariant (subtype)
    • Access modifier cannot be more restrictive (can be less restrictive)
    • Cannot override final, static, or private methods
    • Must use @Override annotation (optional but recommended)
    • Overriding vs Hiding: Overriding is for instance methods (dynamic), hiding is for static methods (static binding)
    • If @Override used incorrectly, compilation error
  9. Define polymorphism in Java. Explain the two types of polymorphism: Compile-time polymorphism (static binding) Runtime polymorphism (dynamic binding)
    • Polymorphism: Ability to take many forms
    • Compile-time: Method overloading - resolved at compile time
    • Example: add(int, int) and add(double, double)
    • Runtime: Method overriding - resolved at runtime
    • Example: Parent reference holding child object, animal.sound() calls childs method
  10. Differentiate beween Method Overloading and Method Overriding.
    
    Feature	                Method Overloading	Method Overriding
    When occurs?	        Within same class	Between parent-child classes
    Inheritance required?	No	                Yes
    Return type rule	    Can be different	Must be same or covariant
    Access modifier rule	Can be different	Cannot be more restrictive
    Annotation used	        None	            Override
    Binding type	        Compile-time	    Runtime